using System;
using System.Collections;
using System.Runtime.InteropServices;
namespace BMC.ARSystem
{
	[ClassInterface, Guid("1d703c62-83e3-4724-a487-298094cb333b"), ProgId("BMC.ARSystem.Session")]
	public class Session
	{
		private string _primaryServerName;
		private string _secondaryServerName;
		private string _userName;
		private string _password;
		private Server.ObjectType _currentObjectType;
		private string _currentLocalForm;
		private string _currentRemoteForm;
		private int _currentActionIndex;
		private Hashtable _currentOpenWindowSampleServerForms;
		private uint _currentFieldId;
		private Hashtable m_servers;
		private static Session _defaultSession;
		public static Session DefaultSession
		{
			get
			{
				if (Session._defaultSession == null)
				{
					Session._defaultSession = new Session();
				}
				return Session._defaultSession;
			}
		}
		internal Server.ObjectType CurrentObjectType
		{
			get
			{
				return this._currentObjectType;
			}
			set
			{
				this._currentObjectType = value;
			}
		}
		internal string CurrentLocalForm
		{
			get
			{
				return this._currentLocalForm;
			}
			set
			{
				this._currentLocalForm = value;
			}
		}
		internal string CurrentRemoteForm
		{
			get
			{
				if (this._currentRemoteForm != null)
				{
					return this._currentRemoteForm;
				}
				return this.CurrentLocalForm;
			}
			set
			{
				this._currentRemoteForm = value;
			}
		}
		internal Hashtable CurrentOpenWindowSampleServerForms
		{
			get
			{
				return this._currentOpenWindowSampleServerForms;
			}
			set
			{
				this._currentOpenWindowSampleServerForms = value;
			}
		}
		internal int CurrentActionIndex
		{
			get
			{
				return this._currentActionIndex;
			}
			set
			{
				this._currentActionIndex = value;
			}
		}
		internal uint CurrentFieldId
		{
			get
			{
				return this._currentFieldId;
			}
			set
			{
				this._currentFieldId = value;
			}
		}
		internal string PrimaryServerName
		{
			get
			{
				return this._primaryServerName;
			}
			set
			{
				this._primaryServerName = value;
			}
		}
		internal string SecondaryServerName
		{
			get
			{
				return this._secondaryServerName;
			}
			set
			{
				this._secondaryServerName = value;
			}
		}
		public Session()
		{
			this.m_servers = new Hashtable();
			if (Session._defaultSession == null)
			{
				Session._defaultSession = this;
			}
		}
		internal static string MakeServerName(string serverName, int tcpPort)
		{
			return string.Format("{0}{1}{2}", serverName, (tcpPort != 0) ? ":" : "", (tcpPort != 0) ? tcpPort.ToString() : "");
		}
		public Server ConnectToServer(string serverName, int tcpPort, string authString, string user, string password, bool ensureAdmin, bool lockToadminQueue)
		{
			string text = Session.MakeServerName(serverName, tcpPort);
			if (!this.m_servers.Contains(text))
			{
				Server server = new Server();
				server.Login(serverName, user, password, authString);
				if (tcpPort != 0)
				{
					server.SetServerPort(tcpPort, 0);
				}
				if (lockToadminQueue)
				{
					server.LockToAdminPort();
				}
				bool flag;
				bool flag2;
				bool flag3;
				server.VerifyUser(out flag, out flag2, out flag3);
				if (ensureAdmin && !flag)
				{
					throw new Exception("Not an administrator. Excepted Administrator user account.");
				}
				this.AddServer(server);
			}
			return (Server)this.m_servers.get_Item(text);
		}
		internal void AddServer(Server server)
		{
			string text = Session.MakeServerName(server.ServerName, server.ServerPort);
			if (this.m_servers.get_Count() == 0)
			{
				this._primaryServerName = text;
				this._userName = server.User;
				this._password = server.Password;
			}
			this.m_servers.set_Item(text, server);
			server.Session = this;
		}
		internal void RemoveServer(Server server)
		{
			server.Logout();
			string text = Session.MakeServerName(server.ServerName, server.ServerPort);
			this.m_servers.Remove(text);
			if (text.Equals(this._primaryServerName))
			{
				this._primaryServerName = null;
			}
		}
		public void DisconnectFromServer(string serverName, int tcpPort)
		{
			string text = Session.MakeServerName(serverName, tcpPort);
			if (!this.m_servers.Contains(text))
			{
				return;
			}
			this.RemoveServer((Server)this.m_servers.get_Item(text));
		}
		public int ServerCount()
		{
			return this.m_servers.get_Count();
		}
		public Hashtable GetServers()
		{
			return this.m_servers;
		}
		public Server GetServer(string serverName, int tcpPort, string authString, string user, string password, bool ensureAdmin, bool lockToadminQueue)
		{
			if (serverName == null || serverName.Equals("@") || serverName.Equals("*"))
			{
				return (Server)this.m_servers.get_Item(this._primaryServerName);
			}
			string text = Session.MakeServerName(serverName, tcpPort);
			if (!this.m_servers.Contains(text))
			{
				this.ConnectToServer(serverName, tcpPort, authString, user, password, ensureAdmin, lockToadminQueue);
			}
			return (Server)this.m_servers.get_Item(text);
		}
		public Server GetServer(string serverName, int tcpPort, string authString, bool ensureAdmin, bool lockToadminQueue)
		{
			Server server = (Server)this.m_servers.get_Item(this._primaryServerName);
			return this.GetServer(serverName, tcpPort, authString, server.User, server.Password, ensureAdmin, lockToadminQueue);
		}
		public Server GetServer(string serverName, int tcpPort, string authString)
		{
			return this.GetServer(serverName, tcpPort, authString, false, false);
		}
		public Server GetServer(string serverName, int tcpPort)
		{
			return this.GetServer(serverName, tcpPort, "", false, false);
		}
		public Server GetServer(string serverName)
		{
			return this.GetServer(serverName, 0);
		}
	}
}
